package com.wisedu.w5autocode.popup.actions;

import java.io.File;
import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.eclipse.jdt.internal.core.PackageFragment;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;


@SuppressWarnings("restriction")
public class OpenExplorerAction implements IWorkbenchWindowActionDelegate
{
	private Object selected = null;
	@SuppressWarnings({ "unchecked", "unused" })
	private Class selectedClass = null;

	@Override
	public void init(IWorkbenchWindow window)
	{
	}

	@Override
	public void run(IAction action)
	{
		if (!"unknown".equals(this.selected))
		{
			File directory = null;
			if (this.selected instanceof IResource)
			{
				directory = new File(((IResource) this.selected).getLocation().toOSString());
			} else if (this.selected instanceof File)
			{
				directory = (File) this.selected;
			}

			if (this.selected instanceof IFile)
			{
				directory = directory.getParentFile();
			}
			if (this.selected instanceof File)
			{
				directory = directory.getParentFile();
			}
			String target = "explorer.exe " + directory.toString();

			try
			{
				Runtime.getRuntime().exec(target);
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection)
	{
		if (selection instanceof IStructuredSelection)
		{
			Object obj = ((IStructuredSelection) selection).getFirstElement();
			if(obj instanceof IAdaptable)
			{
				IAdaptable adaptable  = (IAdaptable)obj ;
				this.selectedClass = adaptable.getClass();
				if (adaptable instanceof IResource)
				{
					this.selected = ((IResource) adaptable);
				} else if ((adaptable instanceof PackageFragment) && (((PackageFragment) adaptable).getPackageFragmentRoot() instanceof JarPackageFragmentRoot))
				{
					this.selected = getJarFile(((PackageFragment) adaptable).getPackageFragmentRoot());
				} else if (adaptable instanceof JarPackageFragmentRoot)
				{
					this.selected = getJarFile(adaptable);
				} else
				{
					this.selected = ((IResource) adaptable.getAdapter(IResource.class));
				}
			}else
			{
				this.selected = "unknown";
			}
		} else
		{
			this.selected = "unknown";
		}
	}

	protected File getJarFile(IAdaptable adaptable)
	{
		JarPackageFragmentRoot jpfr = (JarPackageFragmentRoot) adaptable;
		File selected = jpfr.getPath().makeAbsolute().toFile();
		if (!(selected.exists()))
		{
			File projectFile = new File(jpfr.getJavaProject().getProject().getLocation().toOSString());
			selected = new File(projectFile.getParent() + selected.toString());
		}
		return selected;
	}

	@Override
	public void dispose()
	{
	}

}